/**
 * 配合jquery-ui dialog,ajax获取dialog内容
 */
(function($) {
	$.fn.AjaxDialog = function(option) {
		//初始化配置项
		option = $.extend({}, $.fn.AjaxDialog.option, option);
		
		return this.click(function() {
				var random = Math.random() * 1000000;
				var dialogId = Math.ceil(random);
				var url = option.url;
				
				$(this).attr('dialog-id', dialogId);
				
				//ajax请求url获取内容
				$.ajax({
					cache:false,
					dataType:'html',
					url:url,
					type:'get',
					success:function(content) {
						content = '<div id="sid' + dialogId + '" class="dialog-box">' + content + '</div>';
						$(document.body).append(content);
						/* 加载dialog */
						$('#sid' + dialogId).dialog({
							modal:true,
							resizable:false,
							autoOpen:true,
							closeText:'关闭',
							draggable:true,
							title:option.title,
							close:function(event, ui) { 
								$('#sid' + dialogId).dialog('destroy');//摧毁dialog
								$('#sid' + dialogId).detach();
								option.close(event, ui);
							},
							create:option.create,
							width:option.width,
							height:option.height
						});
						/* 打开dialog */
						$('#sid' + dialogId).dialog('open');
					}
				});
		});
	};
	
	$.fn.AjaxDialog.option = {
		title:'对话框',
		url:'',
		width:700,
		height:450,
		close:function() {},
		create:function() {}
	};
})(jQuery);